﻿package  kr.prev.utils
{
	import flash.utils.ByteArray;
	
	public class PrUtil {

		public static function similar(data1:Number,data2:Number,errorLimit:Number):Boolean {
			if (data1 + errorLimit >= data2 && data1 - errorLimit <= data2) {
				return true
			}else if (data2 + errorLimit >= data1 && data2 - errorLimit <= data1) {
				return true
			}else {
				return false
			}
		}
		
		
		public static function setTimeModify(second:Number):String {
			return PrUtil.set0(Math.floor(second/60)) + ':' + PrUtil.set0(Math.floor(second%60))
		}
		
		public static function set0(data:int , length:uint=2):String {
			if (length < String(data).length) return String(data)
			
			var tempData:String = ''
			length -= String(data).length
			
			while (length > 0) {
				tempData += '0'
				length--
			}
			return tempData + String(data);
		}
		
		
		public static function convertEncoding(data:String,originCharset:String,covertingCharset:String):String {
			var tmp:ByteArray = new ByteArray();
			if(originCharset != null) {
				tmp.writeMultiByte( data , originCharset )
			}else {
				tmp.writeUTFBytes( data )
			}
			tmp.position = 0
			if (covertingCharset != null) {
				return tmp.readMultiByte( tmp.length , covertingCharset)
			}else {
				return tmp.readUTFBytes( tmp.length )
			}
			
		}
		
		public static function overflow(data:Number, minimum:Number, maximum:Number):Number {
			if (minimum > maximum) return NaN
			
			var _tempData:Number = data
			while(_tempData < minimum || _tempData > maximum ) {
				if (_tempData < minimum) {
					_tempData += maximum - minimum
				}else if (_tempData > maximum) {
					_tempData -= maximum - minimum
				}
			}
			return _tempData
		}
		
	}
	
}
